/*
 * IntSet.java
 * 
 * Copyright 2004 Christoph Csallner and Yannis Smaragdakis.
 */
package edu.gatech.cc.cnc.params;

import static edu.gatech.cc.jcrasher.Assertions.check;
import de.fhg.first.fd.Domain;
import de.fhg.first.fd.X;
import edu.gatech.cc.jcrasher.plans.expr.Expression;
import edu.gatech.cc.jcrasher.plans.expr.literals.IntLiteral;

/**
 * Represents an int variable, which includes array indeces.
 * 
 * @author csallner@gatech.edu (Christoph Csallner)
 */
public class ParamInt extends ParamIntegral<Integer> {

	/**
	 * @return int domain
	 */
	public static Domain getDomain() {
		return intDomain;
	}
	
	/**
	 * Constructor, regular int.
	 */
	public ParamInt(String name) {
		this (getDomain().getMin(), getDomain().getMax(), name);
	}

	/** 
	 * Constructor, may pass [0, MAX] for array index.
	 * pre: resetSolver()
	 * pre: isInDomain(min)
	 * pre: isInDomain(max)
	 */
	public ParamInt(int min, int max, String name) {
		super(min, max, name, intDomain);		
		check(isInDomain(min));
		check(isInDomain(max));
	}
	
	
	/**
	 * @return val is within the domain used for int.
	 */
	protected boolean isInDomain(int val) {
		return getDomain().getMin()<=val && val<=getDomain().getMax();
	}
	
	
	/**
	 * @return variable (!= null) iff constant in domain.
	 */
	@Override
	public X parse(String constant) {
		int i;
		try {
		 i = Integer.parseInt(constant);}
		catch(NumberFormatException e) {
			return parseError(constant);
		}
		
		if (isInDomain(i))
			return solver.newX(i);
		
		return tooLargeForSolver(constant);
	}

	
	/**
	 * Assumes that constraint system solution to which var belongs has been
	 * first prepared and then generated before.
	 */
	@Override
	public Expression<Integer> getPlanExpression() {
		return new IntLiteral(getValue());
	}
	
	@Override
	public Class<Integer> getType() {
		return Integer.TYPE;
	}
}
